﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Command class for "touching" a file, i.e., creating a file.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;

namespace FileSystem.Impl.Commands
{
    class Touch : ICommand, ICommandPrototype
    {
	    private static Touch zombie;
	
	    private static void loadEmulator() {
            zombie = new Touch(new HelpDescription() { Name = "touch", Description = "Creates a file in current directory." });
		    ICommandPrototypeRegistry r = AppResources.getInstance().getCommandRegistry();
		    r.register("touch", zombie);
	    }
	
	    private IFSDirectory cwd;
	    private String filename;
	    private bool executeSucceeded;
        public IHelpDescription Description
        {
            get;
            private set;
        }
        private Touch(IHelpDescription helpDescription)
        {
		    //for zombie
            this.Description = helpDescription;
	    }
	
	    private Touch(String filename) {
		    this.filename = filename;
		    executeSucceeded = false;
	    }
	
	    public ICommand clone() {
		    //Cannot clone touch without name of file as arg.
		    return null;
	    }
	
	    public ICommand clone(String arg) {
		    Touch rc = new Touch(arg);
		    return rc;
	    }

	    public IResult execute() {
            this.cwd = AppResources.getInstance().getFileSystem().CurrentDirectory;
		    INodeMetaDataVO metaData = AppResources.getInstance().getFileSystem().getMetaDataForEmptyNode(filename);
		    FSFile file = new FSFile(metaData, "");
		    bool rc = cwd.addNode(file);
		    if (rc) {
			    executeSucceeded = true;
			    return new Result("touch successful: " + file.describe());
		    }
		    executeSucceeded = false;
		    return new Result(true, "touch failed.");
	    }
	
	    public void undo() {
		    if (!executeSucceeded) {
			    //nothing to do, because execution failed...
			    //we don't want to remove the node that might have been there of the same name.
			    return;
		    }
		    cwd.removeNode(filename);
	    }
	
	    public IResult redo() {
		    return execute();
	    }
	
	    public String describe(IPrintContext context) {
		    return context.getFormattedString("touch " + filename + "(" + executeSucceeded + ").");
	    }
    }
}
